﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Xml.Serialization;

// 订单明细类
class OrderDetail
{
    public int Id { get; set; }
    public string ProductName { get; set; }
    public double Price { get; set; }
    public int Quantity { get; set; }

    public override string ToString()
    {
        return $"Product: {ProductName}, Price: {Price}, Quantity: {Quantity}";
    }
}

// 订单类
class Order
{
    public int OrderId { get; set; }
    public string CustomerName { get; set; }
    public List<OrderDetail> Details { get; set; } = new List<OrderDetail>();

    public double TotalAmount => Details.Sum(detail => detail.Price * detail.Quantity);

    public override string ToString()
    {
        return $"Order ID: {OrderId}, Customer: {CustomerName}, Total Amount: {TotalAmount}";
    }
}

// 订单服务类
  class OrderService
{
    private List<Order> orders = new List<Order>();

    // 添加订单
    public void AddOrder(Order order)
    {
        if (orders.Any(o => o.OrderId == order.OrderId))
        {
            throw new InvalidOperationException("Order already exists.");
        }
        orders.Add(order);
    }

    // 删除订单
    public void RemoveOrder(int orderId)
    {
        var order = orders.FirstOrDefault(o => o.OrderId == orderId);
        if (order == null)
        {
            throw new InvalidOperationException("Order not found.");
        }
        orders.Remove(order);
    }

    // 修改订单
    public void ModifyOrder(Order modifiedOrder)
    {
        var order = orders.FirstOrDefault(o => o.OrderId == modifiedOrder.OrderId);
        if (order == null)
        {
            throw new InvalidOperationException("Order not found.");
        }
        order.CustomerName = modifiedOrder.CustomerName;
        order.Details = modifiedOrder.Details;
    }

    // 查询订单
    public List<Order> QueryOrders(Func<Order, bool> condition)
    {
        return orders.Where(condition).ToList();
    }

    // 排序订单
    public void SortOrders(IComparer<Order> comparer)
    {
        orders.Sort(comparer);
    }
    public void Import(string filePath)
    {
        try
        {
            var serializer = new XmlSerializer(typeof(List<Order>));
            using (var reader = new StreamReader(filePath))
            {
                var loadedOrders = (List<Order>)serializer.Deserialize(reader);
                orders.AddRange(loadedOrders);
            }
            Console.WriteLine($"Orders imported from {filePath} successfully.");
        }
        catch (Exception ex)
        {
            Console.WriteLine($"Error importing orders: {ex.Message}");
        }
    }
}

}

class Program
{
    static void Main()
    {
        // 示例用法
        var orderService = new OrderService();

        var order1 = new Order { OrderId = 1, CustomerName = "Alice" };
        order1.Details.Add(new OrderDetail { Id = 1, ProductName = "Apple", Price = 2.5, Quantity = 10 });
        orderService.AddOrder(order1);

        var order2 = new Order { OrderId = 2, CustomerName = "Bob" };
        order2.Details.Add(new OrderDetail { Id = 2, ProductName = "Banana", Price = 1.8, Quantity = 20 });
        orderService.AddOrder(order2);

        // 查询订单
        var queryResult = orderService.QueryOrders(order => order.CustomerName == "Alice");
        foreach (var result in queryResult)
        {
            Console.WriteLine(result);
        }
    }
}
